//
//  ZLBaseResourceUtils.m
//
//  Created by Jiaozl on 2021/4/20.
//  
//

#import "ZLBaseResourceUtils.h"
#import "NSObject+ZLIOSKit.h"

@implementation ZLBaseResourceUtils

#pragma mark - 需要子类重写zlIOSkit_bundleName方法

+ (UIImage *)zlIOSkit_imageWithName:(NSString *)imageName {
    return [self zlIOSkit_imageWithName:imageName bundleName:[self zlIOSkit_bundleName] pathClass:self];
}

+ (NSString *)zlIOSkit_jsonPathWithName:(NSString *)jsonName {
    return [self zlIOSkit_jsonPathWithName:jsonName bundleName:[self zlIOSkit_bundleName] pathClass:self];
}

+ (NSString *)zlIOSkit_filePathWithName:(NSString *)fileName ofType:(NSString *)type {
    return [self zlIOSkit_filePathWithName:fileName ofType:type bundleName:[self zlIOSkit_bundleName] pathClass:self];
}

+ (NSBundle *)zlIOSkit_bundle {
    return [self zlIOSkit_bundleWithName:[self zlIOSkit_bundleName] pathClass:self];
}

#pragma mark - 

+ (NSString *)zlIOSkit_jsonPathInMainBundleWithName:(NSString *)jsonName {
    return [self zlIOSkit_jsonPathWithName:jsonName bundleName:@"" pathClass:self];
}

#pragma mark -

+ (UIImage *)zlIOSkit_imageWithName:(NSString *)imageName bundleName:(NSString *)bundleName pathClass:(Class)aClass {
    if (![imageName zlIOSkit_isValidStr]) {
        return nil;
    }
    UIImage *img = [UIImage imageNamed:imageName inBundle:[self zlIOSkit_bundleWithName:bundleName pathClass:aClass] compatibleWithTraitCollection:nil];
    return img;
}

+ (NSString *)zlIOSkit_jsonPathWithName:(NSString *)jsonName bundleName:(NSString *)bundleName pathClass:(Class)aClass {
    if (![jsonName zlIOSkit_isValidStr]) {
        return nil;
    }
    NSString *path = [self zlIOSkit_filePathWithName:jsonName ofType:@"json" bundleName:bundleName pathClass:aClass];
    return path;
}

+ (NSString *)zlIOSkit_filePathWithName:(NSString *)fileName ofType:(NSString *)type bundleName:(NSString *)bundleName pathClass:(Class)aClass {
    if (![fileName zlIOSkit_isValidStr] || ![type zlIOSkit_isValidStr]) {
        return nil;
    }
    return [[self zlIOSkit_bundleWithName:bundleName pathClass:aClass] pathForResource:fileName ofType:type];
}

+ (NSBundle *)zlIOSkit_bundleWithName:(NSString *)bundleName pathClass:(nonnull Class)aClass {
    if (![bundleName zlIOSkit_isValidStr]) {
        return [NSBundle mainBundle];
    }

    NSString *bundlePath = [[NSBundle bundleForClass:aClass].resourcePath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.bundle", bundleName]];
    return [NSBundle bundleWithPath:bundlePath];
}

#pragma mark -

+ (id)jsonDataWithPath:(NSString *)path {
    NSData *data = [NSData dataWithContentsOfFile:path];
    if (data == nil) {
        return nil;
    }
    id JsonObject = [NSJSONSerialization JSONObjectWithData:data
                                                    options:NSJSONReadingAllowFragments
                                                      error:nil];
    return JsonObject;
}

#pragma mark -

/**
 * 需要子类化并重写此方法
 * @return 返回子类所在组件的bundle名称
 */
+ (NSString *)zlIOSkit_bundleName {
    NSString *errorMsg = [NSString stringWithFormat:@"You must override %@ in a subclass of %@.", NSStringFromSelector(_cmd), NSStringFromClass(ZLBaseResourceUtils.class)];
    NSAssert([self isMemberOfClass:ZLBaseResourceUtils.class], errorMsg);
    return @"";
}

@end
